<?php

	include_once('../sys/sys.php');
	include_once('dm5.php');
	header('Content-Type: text/html; charset=utf-8');
	
	$action = $_GET['action'];
	$spc = $_GET['spc'];
	if($action != 'acr'){
		show_normal_mesR('操作错误','操作错误','操作错误');
	}
	//获取已经完结的和未完结的总页面数
//	$wjcontent = file_get_contents("http://www.nonadm.com/list/zt/wj_1.htm");
//	$wjc = $wjc[1];
//	$wjc = explode('页 当前为第',$wjc);
//	$wjc = explode('<div class="tys">[共<SPAN>',$wjcontent);
//	$twjc = $wjc[0];
//	unset($wjc);
//	unset($wjcontent);
	$lzcontent = file_get_contents("http://cnc.dm5.com/SerializeComic.aspx");
	$lzc = explode('个连载 共有',$lzcontent);
	$lzc = $lzc[1];
	$lzc = explode('页 当前为第',$lzc);
	$tlzc = trim($lzc[0]);
	unset($lzc);
	unset($lzcontent);
	
	
	//开始整站采集
	//定义系统预置URL
	//完结的
//	$pre_wj_url = "http://www.nonadm.com/list/zt/wj_";
	$pre_lz_url = "http://cnc.dm5.com/SerializeComic.aspx?page=";
	$end_flg = '';
	if($spc == 'wj'){
		//getwj($twjc,$pre_wj_url,$end_flg);
		echo '由于效率太低，现已经停止采集完结版本。';
	}else if($spc == 'lz'){
		getlz($tlzc,$pre_lz_url,$end_flg);
	}else if($spc == 'all'){
		//getlz($tlzc,$pre_wj_url,$end_flg);
		//getwj($twjc,$pre_wj_url,$end_flg);
	}
	
	echo "采集完毕";
	
	ob_end_flush();
	
	
	//以下为函数定义
	
	
	
	function getwj($count,$pre,$end){
		//开始循环采集每个页面
		for($i = 1;$i < $count ;$i++){
			$content = mb_convert_encoding(file_get_contents($pre . $i . $end),"UTF-8","GB2312");
			$content = explode('<DIV class=lastnew02 id=lastnew01 style="DISPLAY: none">',$content);
			$content = explode('<div class="lastnew02" id="lastnew02"> ',$content[1]);
			$content = $content[0];
			$sstr = array('<UL class=cmimg_lastimg02><LI><A class=cmimg02 ','target=_blank','</A> </LI><LI><H2><A title=');
			$rstr = array("\r\n","\r\n","\r\n");
			$content = str_replace($sstr,$rstr,$content);
			
			//直接获取连接
			$match_str = "/title=(.*) href=\"(.*)\"/isU";
			preg_match_all($match_str,$content,$urln);
			$urln = $urln[2];
			foreach($urln as $k=>$v){
				$thisurl = "http://www.nonadm.com" . $v . "/";
				//开始单页采集
				ob_flush();
				flush();
				echo '当前开始采集' . $thisurl . "<br>";
				
				//continue;
				

				getsig($thisurl);
			}
		}
		unset($content);
		
	}
	
	function getlz($count,$pre,$end){
		//开始循环采集每个页面
		for($i = 1;$i < $count ;$i++){
			$content = file_get_contents($pre . $i . $end);
			$content = explode('<table width="100%"><tr><td align="center">',$content);
			$content = explode('</td></tr></table>',$content[1]);
			$content = $content[0];
//			$content = str_replace("\r\n",'',$content);
			$sstr = array('" /></a></div>','class="bk" width="150" height="200" border="0" alt="','<div id="pic"><img src=');
//			$rstr = array("\r\n");
			$content = str_replace($sstr,"\r\n",$content);
			//直接获取连接
			$match_str = "/<a href=\"Type.aspx\?id=(.*)\">/isU";
			preg_match_all($match_str,$content,$urln);
			$urln = $urln[1];
			foreach($urln as $k=>$v){
				if ($k%2==0) {
					continue;
				}
				$thisurl = "http://cnc.dm5.com/Type.aspx?id=" . $v;
				//开始单页采集
				ob_flush();
				flush();
				echo '当前开始采集' . $thisurl . "<br>";
				getsig($thisurl);
			}
		}
		unset($content);
	}
	
	function getsig($url){
		global $DB;
		$id = explode("=",$url);
		$id = $id[1];
		$tid = $id;
			
		//定义公用变量
		$url = 'http://cnc.dm5.com/Type.aspx?id=' . $tid;
		$url_pre = 'http://cnc.dm5.com/';
		
		
		//分离URL以及ID
		$id = $tid;
			
			
		//获取URL内容
	//	$url = "./data/1.htm";
		$content = 	file_get_contents($url);
	//	$content = 	mb_convert_encoding(file_get_contents($url),"UTF-8","GB2312");
		
		//获取名称
		$name = explode('<span class="redzic">',$content);
		$name = $name[1];
		$name = explode('</span></div><div class="sya02">',$name);
		$name = $name[0];
		
		//获取最后更新时间
		$dt = explode('</a><br />更新时间：',$content);
		$dt = $dt[1];
		$dt = explode('<br /><b>',$dt);
		$dt = strtotime(trim($dt[0]));
		if ($dt <= 0) {
			$dt = 0;
		}
	
		//获取URL列表
		$urls = explode('</div><div class="syd">',$content);
		$urls = $urls[1];
		$urls = explode('</div><div class="ff" style="height:6px;">',$urls);
		$urls = $urls[0];
		//$replacer = array('target="_blank"','</a></td></tr><tr><td align="center" >','</a></td><td align="center">','</a></td><td align="center" >');
		//$urls = str_replace($replacer,'',$urls);
		$match_str = "/<a href=\"(.*)\" >/isU";
		preg_match_all($match_str,$urls,$urln);
		$urln = $urln[1];
		$current_total_num = count($urln);
		
		
		//进入每页进行处理
		
		//插入分类信息
			//检查当前ID是否存在
			$where = 'AND tid= ' . $id .'';
			$result = $DB->build_and_exec_query(
				array('select' => 'COUNT(*) as max','from'   => 'dm5tags_setting','where'	=> "1 " . $where ,)
			);	
			//当已经存在的时候更新日期
			if ($result['max'] > 0) {
				
			}
			//当不存在时插入
			else{
				do_taginsert($id,$dt,$name,0);
			}
		//获取数据库COUNT
		$where = 'AND tid= ' . $id .'';
			$result = $DB->build_and_exec_query(
				array('select' => 'num','from'   => 'dm5tags_setting','where'	=> "1 " . $where ,)
			);	
		$dbcount = $result['num'];
		
		foreach ($urln as $k=>$v) {
			//当COUNT小于已经采集的时候，跳过。
			if ($k < $dbcount) {
				continue;
			}
			$current_url = $url_pre . $v;
			//分离ID
			$ttid = explode('showcomic',$current_url);
			$ttid = $ttid[1];
			
			$pcurl = $url_pre . 'display.aspx?id=' . $ttid;
            //使用当前SESSION伪装访问
            cc($pcurl );
			
			//获取每个集的总页数
			$total = get_picurl($ttid,0,true) - 3;
			//采集每个集的内容并入库
			for ($i = 0; $i < $total; $i++) {
				//图片链接入库
				get_picurl($ttid,$i);
				do_photoinsert($id,get_picurl($ttid,$i));
			}
			
		}
		//更新计数
		$where = 'tid= ' . $id .'';
		$DB->do_update('dm5tags_setting',
			array(					  
			   'lastupdate'				=> $dt,
			   'num'					=> count($urln),
			 ),
			 $where
		);
	}
	
	function do_taginsert($id,$lu,$name,$count){
		global $DB;
		if ($lu <= 0||$lu == '') {
			$lu = 0;
		}
		$result = $DB->do_insert( 'dm5tags_setting', array( 'tid'			=> $id,
								   'lastupdate'				=> $lu,
								   'name'					=> $name,
								   'num'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	function do_photoinsert($tid,$url){
		global $DB;
		$result = $DB->do_insert( 'dm5photos', array( 
								   'tid'				=> $tid,
								   'url'					=> $url,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	
?>